package com.chunlei.check.utils;

import org.apache.commons.pool2.impl.GenericObjectPoolConfig;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

/**
 * @author 朝花不迟暮
 * @version 1.0
 * @date 2020/11/9 19:25
 */
@Component
public class JedisUtil
{
    private final Logger log = LoggerFactory.getLogger(this.getClass());
    //@Autowired
    private final JedisPool jedisPool;

    public JedisUtil()
    {
        GenericObjectPoolConfig<Object> config = new GenericObjectPoolConfig<>();        // 设置最大空闲数
        config.setMaxIdle(300);
        // 设置最大连接数
        config.setMaxTotal(1000);
        // 设置最大等待时间
        config.setMaxWaitMillis(30000);
        // 空闲时检查有效性
        config.setTestOnBorrow(true);

//        jedisPool = new JedisPool(config, "192.168.0.118", 6379);
        jedisPool = new JedisPool(config, "127.0.0.1", 6379);

    }

    private Jedis getJedis()
    {
        return jedisPool.getResource();
    }

    /**
     * 设值
     */
    public String set(String key, String value)
    {
        Jedis jedis = null;
        try
        {
            jedis = getJedis();
            return jedis.set(key, value);
        } catch (Exception e)
        {
            log.error("set key:{} value:{} error:{}", key, value, e);
            return null;
        } finally
        {
            close(jedis);
        }
    }

    /**
     * 设有过期时间的值
     *
     * @param expireTime 过期时间, 单位: s
     */
    public String set(String key, String value, int expireTime)
    {
        Jedis jedis = null;
        try
        {
            jedis = getJedis();
            return jedis.setex(key, expireTime, value);
        } catch (Exception e)
        {
            log.error("set key:{} value:{} expireTime:{} error:{}", key, value, expireTime, e);
            return null;
        } finally
        {
            close(jedis);
        }
    }

    /**
     * 取值
     */
    public String get(String key)
    {
        Jedis jedis = null;
        try
        {
            jedis = getJedis();
            return jedis.get(key);
        } catch (Exception e)
        {
            log.error("get key:{} error:{}", key, e);
            return null;
        } finally
        {
            close(jedis);
        }
    }

    /**
     * 删除key
     */
    public Long del(String key)
    {
        Jedis jedis = null;
        try
        {
            jedis = getJedis();
            return jedis.del(key.getBytes());
        } catch (Exception e)
        {
            log.error("del key:{} error:{}", key, e);
            return null;
        } finally
        {
            close(jedis);
        }
    }

    /**
     * 判断key是否存在
     */
    public Boolean exists(String key)
    {
        Jedis jedis = null;
        try
        {
            jedis = getJedis();
            return jedis.exists(key.getBytes());
        } catch (Exception e)
        {
            log.error("exists key:{} error:{}", key, e);
            return null;
        } finally
        {
            close(jedis);
        }
    }

    /**
     * 设值key过期时间
     *
     * @param expireTime 过期时间, 单位: s
     */
    public Long expire(String key, int expireTime)
    {
        Jedis jedis = null;
        try
        {
            jedis = getJedis();
            return jedis.expire(key.getBytes(), expireTime);
        } catch (Exception e)
        {
            log.error("expire key:{} error:{}", key, e);
            return null;
        } finally
        {
            close(jedis);
        }
    }

    /**
     * 获取剩余时间
     */
    public Long ttl(String key)
    {
        Jedis jedis = null;
        try
        {
            jedis = getJedis();
            return jedis.ttl(key);
        } catch (Exception e)
        {
            log.error("ttl key:{} error:{}", key, e);
            return null;
        } finally
        {
            close(jedis);
        }
    }

    private void close(Jedis jedis)
    {
        if (null != jedis)
        {
            jedis.close();
        }
    }
}
